package com.gnerv.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.cms.common.Result;
import com.gnerv.cms.dto.*;
import com.gnerv.cms.service.IMenuService;
import com.gnerv.cms.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础平台-菜单表 前端控制器
 *
 * @author zhang
 * @date 2019/11/28
 **/
@Api(tags = "菜单管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        menuService.saveMenu(menuSaveDTO);
        return new Result();
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        menuService.createMenu(menuSaveDTO);
        return new Result();
    }

    @RequestMapping(value = "/delete/{menuId}", method = RequestMethod.GET)
    public Result deleteMenu(@PathVariable("menuId") String menuId) {
        menuService.deleteMenu(menuId);
        return new Result();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        menuService.updateMenu(menuSaveDTO);
        return new Result();
    }

    @RequestMapping(value = "/{menuId}/status/{status}", method = RequestMethod.GET)
    public Result updateMenuStatus(@PathVariable("menuId") String menuId, @PathVariable("status") Integer status) {
        menuService.updateMenuStatus(menuId, status);
        return new Result();
    }

    @RequestMapping(value = "/select/{ukId}", method = RequestMethod.GET)
    public Result selectMenu(@PathVariable("ukId") String menuId) {
        MenuDTO menuDTO = menuService.selectMenu(menuId);
        return new Result().data("menu", menuDTO);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listMenu(MenuSearchDTO searchDTO,
                           @RequestParam(name = "current", defaultValue = "1") Integer current,
                           @RequestParam(name = "size", defaultValue = "300") Integer size) {
        Page<MenuDTO> menus = menuService.listMenu(searchDTO, current, size);
        return new Result().data("menus", menus);
    }

    @RequestMapping(value = "/list/tree/{parentId}", method = RequestMethod.GET)
    public Result listMenuTree(@PathVariable("parentId") String parentId) {
        List<MenuTreeDTO> menuTree = menuService.listMenuTree(parentId);
        return new Result().data("menuTree", menuTree);
    }

    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Result bindMenuPermission(@RequestBody MenuPermissionDTO menuPermissionDTO) {
        menuService.bindMenuPermission(menuPermissionDTO.getMenuId(), menuPermissionDTO.getPermissionIds());
        return new Result();
    }

    @RequestMapping(value = "list/user/system", method = RequestMethod.GET)
    public Result listSystemByUserId() {
        UserLoginDTO loginUser = ShiroUtils.getLoginUser();
        List<MenuDTO> menus = menuService.listSystemByUserId(loginUser.getUkId());
        return Result.response().data("menus", menus);
    }

    @RequestMapping(value = "/list/tree/system/{systemId}", method = RequestMethod.GET)
    public Result listMenuTreeBySystemUserId(@PathVariable("systemId") String systemId) {
        UserLoginDTO loginUser = ShiroUtils.getLoginUser();
        List<MenuTreeDTO> menuTree = menuService.listMenuTreeBySystemIdAndUserId(systemId, loginUser.getUkId());
        return new Result().data("menuTree", menuTree);
    }

}
